/**
 * Source file: Validation.ejs
 * Copyright (C) 2013 : Quantum Solutions
 * Id: $Id: Validation.ejs 54 2013-04-20 09:22:08Z mr.robert.ellis $
 * Date: $Date: 2013-04-20 09:22:08 +0000 (Sat, 20 Apr 2013) $
 */
function Field_onBlur()
{
    if(event.target.name=="IDnumber") {
		if (!IDNoValidater(EFormGetValue("IDnumber")) && EFormGetValue("IDnumber") != ""){
			app.alert("Main member IDnumber is invalid: " + EFormGetValue("IDnumber"));				
			EFormSetFocus(EFormGetField("IDnumber"));  
		} 
    } else if(event.target.name=="DIDnumber") {
		if (!IDNoValidater(EFormGetValue("DIDnumber")) && EFormGetValue("DIDnumber") != ""){
			app.alert("Deceased ID number is invalid: " + EFormGetValue("DIDnumber"));				
			EFormSetFocus(EFormGetField("DIDnumber"));  
		} 
    } else if(event.target.name=="CIDnumber") {
		if (!IDNoValidater(EFormGetValue("CIDnumber")) && EFormGetValue("CIDnumber") != ""){
			app.alert("Claimant ID number is invalid: " + EFormGetValue("CIDnumber"));				
			EFormSetFocus(EFormGetField("CIDnumber"));  
		} 
    } else if(event.target.name=="PDIDnumber") {
		if (!IDNoValidater(EFormGetValue("PDIDnumber")) && EFormGetValue("PDIDnumber") != ""){
			app.alert("ID number is invalid: " + EFormGetValue("PDIDnumber"));				
			EFormSetFocus(EFormGetField("PDIDnumber"));  
		} 
    } else if(event.target.name=="PDBranchCode") {
		ValidateBankDetails();
	}
	
    return true;
}

function adddouble(number){
	if (number>9) {
		num1 = parseInt((number/10));
		num2 = parseFloat((number/10) - num1);
		num2 = parseInt(Math.round(num2*10));

		number = num1 + num2;
	}
	return number;
}
function IDNoValidater(idno) {
	idno = idno.split('');
	
	if (idno.length != 13){
		//alert('idnumber should be 13 digits long, but is '+idno.length);
		return false;
	}	
	
	var level1 = 0;
	for (x=0;x<=12;x++) {
		if ((x/2)!=parseInt((x/2))) {
			iddigit = idno[x]*2;
			level1 = level1 + adddouble(iddigit);
		}
	}
	var level2 = 0;
	for (x=0;x<=11;x++){
		if ((x/2)==parseInt((x/2))) {
			iddigit = parseInt(idno[x]);
			level2 = level2 + iddigit;
		}
	}

	leveltotal = level1 + level2;
	remainder = leveltotal - (Math.round(leveltotal/10)*10);
	lastdigit = 10 - remainder;

	if (lastdigit>=10) {
		lastdigit = lastdigit - 10;
	}
	valid = (lastdigit==idno[12]);
	/*
	if (!valid)
		alert('lastdigit should be: ' +lastdigit + ' but it is currently='+ idno[12]);
	else
		alert(idno+' is a valid rsa idnum');
	*/
	return valid;
}




function ValidateBankDetails() {
  szSQL = 
   "SELECT bnknme, Bank_Code, Branch_Description, Branch_Clearing_Code "
   +" FROM BANK, Branch WHERE Bank.Bnkcde = branch.Bank_Code AND Branch_Clearing_Code = "+EFormGetValue("PDBranchCode");

app.alert("szSQL "+szSQL);
 
	var ConnectionString = "Provider=SQLOLEDB;Server=127.0.0.1;Database=ciu;";
	app.alert("before oADOCon.Open ");
	var oADOCon = new ActiveXObject("ADODB.Connection");
	app.alert("oADOCon.Open ");
	oADOCon.Open(ConnectionString,"sa","P@55w0rd",-1);
	app.alert("oADOCon.Open "+oADOCon);
    
	var rst = new ActiveXObject("ADODB.Recordset");
	rst.Open(szSQL,oADOCon);
	app.alert("rst.Open "+rst);
	
   if (rst.EOF){
		app.alert("oh poopy.. no records found");
   }
   
   rst.close;
   oADOCon.close;

}


